package com.woniu.hander;

import com.alibaba.fastjson.JSON;
import com.woniu.util.ResData;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginFailedHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        AuthenticationException e)
            throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=UTF-8");

        PrintWriter writer = httpServletResponse.getWriter();

        //传统写法
//        Map map = new HashMap();
//        map.put("code",201);
//        writer.print(JSON.toJSONString(map));
        writer.print(JSON.toJSONString(ResData.LOGIN_FILA));//枚举写法
        writer.flush();
        writer.close();
    }
}
